package com.rain.shoppingscienceproj.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.rain.shoppingscienceproj.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author: Rain
 * Create: 2018/4/28 9:02
 */

public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.MyViewHolder> {

    private List<String> localityList;
    private ItemListenerString itemListenerString;
    private Context mContext;


    public SearchAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setmContext(Context mContext) {
        this.mContext = mContext;
    }


    public void setItemListenerString(ItemListenerString itemListenerString) {
        this.itemListenerString = itemListenerString;
    }


    public void setLocalityList(List<String> localityList) {
        this.localityList = localityList;
        notifyDataSetChanged();
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_search_locality_layout, parent, false);//解决宽度不能铺满
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        String title = localityList.get(position);
        holder.tv_search_item.setText(title);
        holder.tv_search_item.setOnClickListener(v -> itemListenerString.itemListener(title));

    }

    @Override
    public int getItemCount() {
        if (localityList == null)
            return 0;
        else
            return localityList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_search_item)
        TextView tv_search_item;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    public interface ItemListenerString {
        void itemListener(String queryString);
    }
}
